package com.mogist.hqc.module.main.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.CardView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.mogist.hqc.R;
import com.mogist.hqc.entitys.response.MainItemVo;
import com.mogist.hqc.module.main.adapter.CardAdapter;
import com.mogist.hqc.module.place.ui.ScenicActivity;
import com.mogist.hqc.module.route.ui.RouteMainActivity;

import java.util.Objects;

import talex.zsw.basecore.util.DimenTool;
import talex.zsw.basecore.util.glide.GlideTool;

public class CardFragment extends Fragment
{
	private CardView mCardView;
	private ImageView mImageView;
	private TextView mTvTitle, mTvInfo, mTvTag1, mTvTag2, mTvTag3;
	private MainItemVo data;
	private int pos = 0;

	@Nullable @Override public View onCreateView(LayoutInflater inflater,
		@Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
	{
		View view = inflater.inflate(R.layout.fragment_card, container, false);
		mCardView = (CardView) view.findViewById(R.id.mCardView);
		mImageView = (ImageView) view.findViewById(R.id.mImageView);
		mTvTitle = (TextView) view.findViewById(R.id.mTvTitle);
		mTvInfo = (TextView) view.findViewById(R.id.mTvInfo);
		mTvTag1 = (TextView) view.findViewById(R.id.mTvTag1);
		mTvTag2 = (TextView) view.findViewById(R.id.mTvTag2);
		mTvTag3 = (TextView) view.findViewById(R.id.mTvTag3);
		mCardView.setMaxCardElevation(
			mCardView.getCardElevation()*CardAdapter.MAX_ELEVATION_FACTOR);
		if(getArguments() != null)
		{
			pos = getArguments().getInt("pos");
			initData();
		}

		return view;
	}

	public CardView getCardView()
	{
		return mCardView;
	}

	private void initData()
	{
		data = ((MainActivity) Objects.requireNonNull(getActivity())).getCardData(pos);
		GlideTool.loadImgRoundedCornersLeft(mImageView, data.getPosterImg(), DimenTool.dp2px(12f));
		mTvTitle.setText(data.getProName());
		mTvInfo.setText(data.getContext());

		mCardView.setOnClickListener(new View.OnClickListener()
		{
			@Override public void onClick(View v)
			{
				if(data.getType().equals("ROUTE"))
				{
					Intent intent = new Intent(getActivity(), RouteMainActivity.class);
					intent.putExtra("id", data.getId());
					startActivity(intent);
				}
				else
				{
					Intent intent = new Intent(getActivity(), ScenicActivity.class);
					intent.putExtra("id", data.getId());
					startActivity(intent);
				}
			}
		});

		if(data.getTags() != null && !data.getTags().isEmpty())
		{
			mTvTag1.setText(data.getTags().get(0));
			if(data.getTags().size() > 1)
			{
				mTvTag2.setText(data.getTags().get(1));
				if(data.getTags().size() > 2)
				{
					mTvTag3.setText(data.getTags().get(2));
				}
				else
				{
					mTvTag3.setVisibility(View.GONE);
				}
			}
			else
			{
				mTvTag2.setVisibility(View.GONE);
				mTvTag3.setVisibility(View.GONE);
			}
		}
		else
		{
			mTvTag1.setVisibility(View.GONE);
			mTvTag2.setVisibility(View.GONE);
			mTvTag3.setVisibility(View.GONE);
		}
	}
}
